//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.Managers;
using System.Text;
using Tathata.Common.Utils;
using Tathata.Common.BLL.EntityCollections;
using System.ComponentModel;
using Tathata.Common;

namespace Tathata.Web.Modules.Account
{
    public partial class CustomerRegisterControl : BaseNopUserControl
    {
        private void ApplyLocalization()
        {
            RadioButton rbGenderM = CreateUserWizardStep1.ContentTemplateContainer.FindControl("rbGenderM") as RadioButton;
            if (rbGenderM != null)
                rbGenderM.Text = GetLocaleResourceString("RegisterPage.GenderMale");

            RadioButton rbGenderF = CreateUserWizardStep1.ContentTemplateContainer.FindControl("rbGenderF") as RadioButton;
            if (rbGenderF != null)
                rbGenderF.Text = GetLocaleResourceString("RegisterPage.GenderFemale");

            Button StepNextButton = CreateUserWizardStep1.CustomNavigationTemplateContainer.FindControl("StepNextButton") as Button;
            if (StepNextButton != null)
                StepNextButton.Text = GetLocaleResourceString("RegisterPage.StepNextButton");

            CompareValidator PasswordCompare = CreateUserWizardStep1.ContentTemplateContainer.FindControl("PasswordCompare") as CompareValidator;
            if (PasswordCompare != null)
            {
                PasswordCompare.ErrorMessage = GetLocaleResourceString("RegisterPage.EnteredPasswordsDoNotMatch");
                PasswordCompare.ToolTip = GetLocaleResourceString("RegisterPage.EnteredPasswordsDoNotMatch");
            }

            Button ContinueButton = CreateUserForm.CompleteStep.ContentTemplateContainer.FindControl("ContinueButton") as Button;
            if (ContinueButton != null)
                ContinueButton.Text = GetLocaleResourceString("RegisterPage.ContinueButton");
        }

        public void CreatedUser(object sender, EventArgs e)
        {
            RadioButton rbGenderM = (RadioButton)CreateUserWizardStep1.ContentTemplateContainer.FindControl("rbGenderM");
            RadioButton rbGenderF = (RadioButton)CreateUserWizardStep1.ContentTemplateContainer.FindControl("rbGenderF");
            TextBox txtFirstName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtFirstName");
            TextBox txtLastName = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtLastName");
            TextBox txtDateOfBirth = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtDateOfBirth");
            TextBox txtEmail = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName");
            TextBox txtCompany = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtCompany");
            TextBox txtStreetAddress = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtStreetAddress");
            TextBox txtStreetAddress2 = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtStreetAddress2");
            TextBox txtZipPostalCode = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtZipPostalCode");
            TextBox txtCity = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtCity");
            TextBox txtPhoneNumber = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtPhoneNumber");
            TextBox txtFaxNumber = (TextBox)CreateUserWizardStep1.ContentTemplateContainer.FindControl("txtFaxNumber");
            DropDownList ddlCountry = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ddlCountry");
            DropDownList ddlStateProvince = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ddlStateProvince");

            Customer customer = CustomerManager.GetByEmail(txtEmail.Text);
            if (rbGenderM.Checked)
                CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "Gender", "M");
            else
                CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "Gender", "F");
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "FirstName", txtFirstName.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "LastName", txtLastName.Text);
            try
            {
                DateTime dateOfBirth = DateTime.Parse(txtDateOfBirth.Text);
                CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "DateOfBirth", XmlHelper.SerializeDateTime(dateOfBirth));
            }
            catch
            {
            }
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "Company", txtCompany.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "StreetAddress", txtStreetAddress.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "StreetAddress2", txtStreetAddress2.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "ZipPostalCode", txtZipPostalCode.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "City", txtCity.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "PhoneNumber", txtPhoneNumber.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "FaxNumber", txtFaxNumber.Text);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "CountryID", ddlCountry.SelectedItem.Value);
            CustomerAttributeManager.InsertCustomerAttribute(customer.CustomerID, "StateProvinceID", ddlStateProvince.SelectedItem.Value);

          }

        public void CreatingUser(object sender, LoginCancelEventArgs e)
        {

        }

        private void FillCountryDropDowns()
        {
            DropDownList ddlCountry = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ddlCountry");
            ddlCountry.Items.Clear();
            CountryCollection countryCollection = CountryManager.GetAllCountriesForRegistration();
            foreach (Country country in countryCollection)
            {
                ListItem ddlCountryItem2 = new ListItem(country.Name, country.CountryID.ToString());
                ddlCountry.Items.Add(ddlCountryItem2);
            }
        }

        private void FillStateProvinceDropDowns()
        {
            DropDownList ddlCountry = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ddlCountry");
            DropDownList ddlStateProvince = (DropDownList)CreateUserWizardStep1.ContentTemplateContainer.FindControl("ddlStateProvince");
            ddlStateProvince.Items.Clear();
            int countryID = 0;
            if (ddlCountry.SelectedItem != null)
                countryID = int.Parse(ddlCountry.SelectedItem.Value);

            StateProvinceCollection stateProvinceCollection = StateProvinceManager.GetAllByCountryID(countryID);
            foreach (StateProvince stateProvince in stateProvinceCollection)
            {
                ListItem ddlStateProviceItem2 = new ListItem(stateProvince.Name, stateProvince.StateProvinceID.ToString());
                ddlStateProvince.Items.Add(ddlStateProviceItem2);
            }
            if (stateProvinceCollection.Count == 0)
            {
                ListItem ddlStateProvinceItem = new ListItem(GetLocaleResourceString("Common.StateProvinceNonUS"), "0");
                ddlStateProvince.Items.Add(ddlStateProvinceItem);
            }
        }

        protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillStateProvinceDropDowns();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            this.ApplyLocalization();

            if (!Page.IsPostBack)
            {
                if (NopContext.Current.User != null)
                {
                    CustomerManager.Logout();
                    Response.Redirect("~/Register.aspx");
                }
                this.FillCountryDropDowns();
                this.FillStateProvinceDropDowns();
                this.DataBind();
            }
        }
    }
}